﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pyramid.Models;

namespace Pyramid.Controllers
{
	public class MarketZoneController : BaseController
	{
		public ActionResult List()
		{
			return View(Db.MarketZones);
		}

		public ActionResult Create()
		{
			var model = new MarketZone();
			return View("Edit", model);
		}

		public ActionResult Edit(int id)
		{
			var model = Db.MarketZones.First(x => x.ZoneId == id);
			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(MarketZone model)
		{
			if (Db.MarketZones.Any(x => x.ZoneId != model.ZoneId && x.ZoneName == model.ZoneName))
			{
				return JError("区域名称重复，请修改！");
			}

			if (model.ZoneId == 0)
			{
				Db.MarketZones.Add(model);
			}
			else
			{
				Db.Entry(model).State = EntityState.Modified;
			}
			Db.SaveChanges();
			return JSuccess("操作成功！", "tab_bsi_marketzone");
		}

		public ActionResult Delete(int id)
		{
			var model = new MarketZone {ZoneId = id};
			Db.Entry(model).State = EntityState.Deleted;
			Db.SaveChanges();
			return JSuccess("删除成功！", "tab_bsi_marketzone");
		}
	}
}
